﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Assignment2_OrderSystem
{
    public static class SQLAdapter
    {
        public static int VerifyUser(string username, string password)
        {
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            DAL.DBAdapter.ClientsDataTable dt = adapter.VerifyUser(password, username);
            if (dt.Rows.Count == 0)
                return -1;
            else
                return dt[0].ClientID;
        }
        public static int CheckIfUsernameExists(string username)
        {
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            DAL.DBAdapter.ClientsDataTable dt = adapter.CheckIfUsernameExists(username);
            if (dt.Rows.Count == 0)
                return -1;
            else
                return dt[0].ClientID;
        }
        public static int CheckIfMailExists(string email)
        {
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            DAL.DBAdapter.ClientsDataTable dt = adapter.CheckIfMailExists(email);
            if (dt.Rows.Count == 0)
                return -1;
            else
                return dt[0].ClientID;
        }
        public static string InsertUser(User user)
        {
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            int success = adapter.InsertUser(user.m_sUsername, user.m_sFirstname, user.m_sLastname, user.m_sAddress, user.m_sCity, user.m_sCountry, user.m_sPhone, user.m_sCellular, user.m_sMail, user.m_sCreditCard, user.m_iIsAdmin, user.m_sPassword, user.m_sSQ, user.m_sAns);
            if (success == 1)
            {
                DAL.DBAdapter.ClientsDataTable dt = adapter.VerifyUser(user.m_sPassword, user.m_sUsername);
                // Gets his client ID
                int clientID = dt[0].ClientID;
                DAL.DBAdapterTableAdapters.ClientCategoryTableAdapter catAdapter = new DAL.DBAdapterTableAdapters.ClientCategoryTableAdapter();
                // Insert into client categories
                foreach (int item in user.m_lCategories)
                {
                    catAdapter.InsertClientCategories(clientID, item);
                }
                return clientID.ToString();
            }
            else
            {
                return "";
            }
        }
        public static List<string> GetSQandAnswer(string username)
        {
            List<string> l = new List<string>();
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            DAL.DBAdapter.ClientsDataTable dt = adapter.GetUserForgotPasswordDetails(username);
            string question = dt[0].Security_Question;
            string answer = dt[0].Answer;
            l.Add(question);
            l.Add(answer);
            return l;
        }
        public static string GetPassword(string username)
        {
            DAL.DBAdapterTableAdapters.ClientsTableAdapter adapter = new DAL.DBAdapterTableAdapters.ClientsTableAdapter();
            DAL.DBAdapter.ClientsDataTable dt = adapter.CheckIfUsernameExists(username);
            return dt[0].Password;
        }
    }
}